package com.example.npercentcalculator;

import java.math.BigDecimal;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	
	/**
	 * 전체값에서 일부값은 몇 퍼센트
	 */
	EditText et1 = null, et2 = null, et3 = null;
	
	BigDecimal partValue;
	String partValueStr = null;

	BigDecimal wholeValue;
	String wholeValueStr = null;

	BigDecimal resultValue;
	String resultValueStr = null;
	
	
	/**
	 * 전체값의 몇 퍼센트는 얼마
	 */
	EditText eta1 = null, eta2 = null, eta3 = null;
	
	BigDecimal percentValue;
	String percentValueStr = null;

	BigDecimal totalValue;
	String totalValueStr = null;

	BigDecimal resultValue2;
	String resultValueStr2 = null;
	
	
	/**
	 * 숫자를 몇 퍼센트 증가.
	 */	
	EditText etb1 = null, etb2 = null, etb3 = null;
	
	BigDecimal myNumber;
	String myNumberStr = null;
	
	BigDecimal myPercent;
	String myPercentStr = null;
	
	BigDecimal resultValue3;
	String resultValueStr3 = null;
	
	
	/**
	 * 숫자를 몇 퍼센트 감소.
	 */	
	EditText etc1 = null, etc2 = null, etc3 = null;
	
	BigDecimal myNumeric;
	String myNumericStr = null;
	
	BigDecimal myPercentage;
	String myPercentageStr = null;
	
	BigDecimal resultValue4;
	String resultValueStr4 = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		et1 = (EditText) findViewById(R.id.et1);
		et2 = (EditText) findViewById(R.id.et2);
		et3 = (EditText) findViewById(R.id.et3);

		et1.addTextChangedListener(partValueFromWholeValueWatcher);
		et2.addTextChangedListener(partValueFromWholeValueWatcher);
		
		eta1 = (EditText) findViewById(R.id.eta1);
		eta2 = (EditText) findViewById(R.id.eta2);
		eta3 = (EditText) findViewById(R.id.eta3);
		
		eta1.addTextChangedListener(wholeValuePercentWatcher);
		eta2.addTextChangedListener(wholeValuePercentWatcher);

		
		etb1 = (EditText) findViewById(R.id.etb1);
		etb2 = (EditText) findViewById(R.id.etb2);
		etb3 = (EditText) findViewById(R.id.etb3);
		
		etb1.addTextChangedListener(increaseTargetPercentWatcher);
		etb2.addTextChangedListener(increaseTargetPercentWatcher);

		
		etc1 = (EditText) findViewById(R.id.etc1);
		etc2 = (EditText) findViewById(R.id.etc2);
		etc3 = (EditText) findViewById(R.id.etc3);
		
		etc1.addTextChangedListener(decreaseTargetPercentWatcher);
		etc2.addTextChangedListener(decreaseTargetPercentWatcher);

	}
	
	
	
	/**
	 * 전체값에서 일부값은 몇 퍼센트
	 */
	private final TextWatcher partValueFromWholeValueWatcher = new TextWatcher() {

		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			et3.setText("0");
		}

		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

		public void afterTextChanged(Editable s) {
			partValueStr = stringFromEditText(et1);
			partValue = stringToBigDecimalFromEditText(et1);
			
			wholeValueStr = stringFromEditText(et2);
			wholeValue = stringToBigDecimalFromEditText(et2);

			if (!partValueStr.equals(null) && !partValueStr.equals("") && !wholeValueStr.equals(null) && !wholeValueStr.equals("")) {
				BigDecimal calculate = partValue.divide(wholeValue, 2, BigDecimal.ROUND_UP);
				BigDecimal calculateResult = calculate.multiply(new BigDecimal(100));

				resultValueStr = String.valueOf(calculateResult);
				et3.setText(resultValueStr);
			}

		}
	};
	
	
	
	/**
	 * 전체값의 몇 퍼센트는 얼마
	 */
	private final TextWatcher wholeValuePercentWatcher = new TextWatcher() {

		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			eta3.setText("0");
		}

		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

		public void afterTextChanged(Editable s) {
			totalValueStr = stringFromEditText(eta2);
			totalValue = stringToBigDecimalFromEditText(eta2);
			
			percentValueStr = stringFromEditText(eta1);
			percentValue = stringToBigDecimalFromEditText(eta1);
			
			if (!percentValueStr.equals(null) && !percentValueStr.equals("") && !totalValueStr.equals(null) && !totalValueStr.equals("")) {
				BigDecimal calculate = totalValue.multiply(percentValue);
				BigDecimal calculateResult = calculate.divide(new BigDecimal(100), 2, BigDecimal.ROUND_UP);

				resultValueStr2 = String.valueOf(calculateResult);
				eta3.setText(resultValueStr2);
			}

		}
	};
	

	

	
	/**
	 * 숫자를 몇 퍼센트 증가.
	 */
	private final TextWatcher increaseTargetPercentWatcher = new TextWatcher() {

		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			etb3.setText("0");
		}

		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

		public void afterTextChanged(Editable s) {
			myNumberStr = stringFromEditText(etb1);
			myNumber = stringToBigDecimalFromEditText(etb1);
			
			myPercentStr = stringFromEditText(etb2);
			myPercent = stringToBigDecimalFromEditText(etb2);
			
			if (!myNumberStr.equals(null) && !myNumberStr.equals("") && !myPercentStr.equals(null) && !myPercentStr.equals("")) {
				BigDecimal calculate = myPercent.divide(new BigDecimal(100), 2, BigDecimal.ROUND_UP).add(new BigDecimal(1)).multiply(myNumber);
				resultValueStr3 = String.valueOf(calculate);
				etb3.setText(resultValueStr3);
			}

		}
	};
	

	/**
	 * 숫자를 몇 퍼센트 감소.
	 */
	private final TextWatcher decreaseTargetPercentWatcher = new TextWatcher() {

		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			etc3.setText("0");
		}

		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

		public void afterTextChanged(Editable s) {
			myNumericStr = stringFromEditText(etc1);
			myNumeric = stringToBigDecimalFromEditText(etc1);
			
			myPercentageStr = stringFromEditText(etc2);
			myPercentage = stringToBigDecimalFromEditText(etc2);
			
			if (!myNumericStr.equals(null) && !myNumericStr.equals("") && !myPercentageStr.equals(null) && !myPercentageStr.equals("")) {
				BigDecimal calculate0 = myPercentage.divide(new BigDecimal(100), 2, BigDecimal.ROUND_UP);                                //Log.i("calculate0", calculate0.toString());
				BigDecimal calculate1 = calculate0.subtract(new BigDecimal(1));                                                          //Log.i("calculate1", calculate1.toString());
				BigDecimal calculate2 = calculate1.multiply(myNumeric);                                                                  //Log.i("calculate2", calculate2.toString());
				BigDecimal calculate3 = calculate2.multiply(new BigDecimal(-1));                                                         //Log.i("calculate3", calculate3.toString());
				resultValueStr4 = String.valueOf(calculate3);
				etc3.setText(resultValueStr4);
			}

		}
	};
	
	
	public String stringFromEditText(EditText et){
		String str = et.getText().toString();
		String retVal = str;
		return retVal;
	}
	
	public BigDecimal stringToBigDecimalFromEditText(EditText et){
		String str = et.getText().toString();
		BigDecimal retVal;
		if (!str.equals(null) && !str.equals("")) {
			retVal = new BigDecimal(str);
		} else {
			retVal = new BigDecimal(0);
		}
		return retVal;
		
	}
}
